package com.Wheesper.www.WheesperServer.Model;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;

public class Contacts {
	private User _user;
	private Set<Contact> _contacts;
	
	public class Contact implements Comparable<Contact>
	{
		private String _email = "default";
		private String _nickname = "default";
		private String _group = "我的好友";
		private String _remarks = "[无]";
		
		public Contact(String _email) {
			this._email = _email;
		}
		
		public Contact Nickname(String nickname) {
			this._nickname = nickname;
			return this;
		}
		
		public Contact Group(String group) {
			this._group = group;
			return this;
		}
		
		public Contact Remark(String remark) {
			this._remarks = remark;
			return this;
		}
		
		public String eMail() {
			return this._email;
		}
		
		public String Group() {
			return this._group;
		}
		
		public String Nickname() {
			return this._nickname;
		}
		
		public String Remark() {
			return this._remarks;
		}

		@Override
		public int compareTo(Contact o) {
			return this._email.compareTo(o._email);
		}
	}
	
	public Contacts(User _user) {
		this._user = _user;
		_contacts  = new TreeSet<Contact>();
	}
	
	public void addNewContact(User user,UserInfo user_info) {
		String email = user.eMail();
		String nickname = user_info.NickName();
		_contacts.add((new Contact(email).Nickname(nickname)));
	}
	
	public void updateContact(String contact_email,String new_remark,String new_group) {
		Iterator<Contact> iter = _contacts.iterator();
		
		while(iter.hasNext()) {
			Contact contact = iter.next();
			if(contact_email.contentEquals(contact.eMail())) {
				contact.Group(new_group).Remark(new_remark);
				return;
			}
		}
	}
	
	public void delOldContact(User user) {
		_contacts.removeIf(new Predicate<Contact>(){
			public boolean test(Contact t) {
				if(t.eMail() == user.eMail())
					return true;
				return false;
			}	
		});
	}
	
	public User user() {
		return this._user;
	}
	
	public Set<Contact> contacts(){
		return _contacts;
	}

}
